using System;
using System.IO;
using System.Collections.Generic;

namespace ProgrammingLanguageNr1
{
	public class SprakProgram
	{
		public SprakProgram (TextReader stream)
		{
			m_errorHandler = new ErrorHandler();
			Tokenizer tokenizer = new Tokenizer(stream, m_errorHandler);
			List<Token> tokens = tokenizer.getTokens();	
			Parser parser = new Parser(tokens, m_errorHandler);
			parser.process();
			AST ast = parser.getAST();
			SymbolTableBuilder symbolTableBuilder = new SymbolTableBuilder(ast, m_errorHandler);
			symbolTableBuilder.process();
			GlobalScope globalScope = symbolTableBuilder.getGlobalScope();
			
			// Console.WriteLine("Tree: " + ast.getTreeAsString());
			
			m_interpreter = new Interpreter(ast, globalScope, m_errorHandler);
		}
		
		public void run() 
		{
			if (m_errorHandler.getErrors().Count == 0) {
				m_interpreter.run();
			} else {
				Console.WriteLine("Can't run program since it contains errors!");
			}
		}
		
		public void printOutputToConsole() {
			Console.WriteLine("PROGRAM OUTPUT:");
			foreach(string s in m_interpreter.getOutput()) {
				Console.WriteLine(s);
			}
		}
		
		public List<string> getOutput() { return m_interpreter.getOutput(); }
		public ErrorHandler getErrorHandler() { return m_errorHandler; }
		
		Interpreter m_interpreter;
		ErrorHandler m_errorHandler;
	}
}

